<?php defined('_JEXEC') or die();
/**
 * @version		0.9.2
 * @package		CSS Menu
 * @subpackage	Module
 * @copyright	2010 Rockford Solutions, Inc. All rights reserved.
 * @license		GNU/GPL
 */

/**
 * Helper Class
 * 
 * @package		CSS Menu
 * @subpackage	Module
 */
class ModCssMenuHelper {
	/**
	 * Retrieves menu items
	 * 
	 * @param	string	Unique name of menu
	 * @return	array	Menu item list
	 */
	public static function getMenuItems($menutype) {
		$user =& JFactory::getUser();
		
		$query = "SELECT `id`, `name`, `alias`, `link`, `parent`, `sublevel` "
		       . "FROM `#__menu` "
			   . "WHERE `menutype` = '" . $menutype . "' "
			   . "AND `published` = 1 "
			   . "AND `access` <= " . $user->get('aid') . " "
			   . "ORDER BY `parent`, `ordering`";
		
		$db = &JFactory::getDBO();
		$db->setQuery($query);
		
		$items = $db->loadAssocList();
		
		return $items;
	}
	
	/**
	 * Sorts menu items into hierarchical structure
	 * 
	 * @param	array	Menu item list
	 * @return	array	Menu item tree
	 */
	public static function sortMenuItems($items) {
		$tree = array();
		
		foreach ($items as $item) {
			if ($item['sublevel'] == 0) {
				$tree[$item['id']] = $item;
			} elseif ($item['sublevel'] == 1) {
				$tree[$item['parent']]['items'][$item['id']] = $item;
			}
		}
		
		return $tree;
	}
}
